package net.lainiao.morethread.Chapter4.demo7;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/3.
 */
public class Main {
    public static void main(String[] args) {
        ScheduledExecutorService executorService= Executors.newScheduledThreadPool(1);
        System.out.println("Main: Starting at "+new Date());
        Task task=new Task("task");
        ScheduledFuture<?> result=executorService.scheduleAtFixedRate(task,1,2, TimeUnit.SECONDS);
        for (int i = 0; i < 10; i++) {
            System.out.println("Main: Delay "+result.getDelay(TimeUnit.MILLISECONDS));
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        executorService.shutdown();
        try {
            TimeUnit.SECONDS.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main: FInished at :"+new Date());
    }
}
